var t = getApp(), e = t.requirejs("core"), o = t.requirejs("foxui");

Page({
    data: {
        icons: t.requirejs("icons"),
        success: !1,
        successData: {},
        coupon: !1
    },
    onLoad: function(e) {
        this.setData({
            options: e,
            imgUrl: t.globalData.approot
        }), t.url(e);
    },
    onShow: function() {
        this.get_list();
    },
    get_list: function() {
        var t = this;
        e.get("order/pay", t.data.options, function(o) {
            50018 != o.error ? (!o.wechat.success && "0.00" != o.order.price && o.wechat.payinfo && e.alert(o.wechat.payinfo.message + "\n不能使用微信支付!"), 
            t.setData({
                list: o,
                show: !0
            })) : wx.navigateTo({
                url: "/pages/order/details/index?id=" + t.data.options.id
            });
        });
    },
    pay: function(t) {
      var a = e.pdata(t).type, s = this, i = this.data.list.wechat; 
      wx.requestSubscribeMessage({
        tmplIds: ['UNlDWQ-Liob-URypsrA7WZZcRSGLrJJVaMSg_wKEfyA', '9avNbJDoDBh6rUKp55xCMCgH14v5qLTKZrrQ-JTy_Tg'],
        success(res) {
          console.log(res, 889991)
          if (res.errMsg == "requestSubscribeMessage:ok") {
            if (res['UNlDWQ-Liob-URypsrA7WZZcRSGLrJJVaMSg_wKEfyA'] == "accept" || res['9avNbJDoDBh6rUKp55xCMCgH14v5qLTKZrrQ-JTy_Tg'] == "accept") {
              console.log(this, 7777);
             
              e.post("order/pay/checkstock", {
                id: s.data.options.id
              }, function (t) {
                0 == t.error ? "wechat" == a ? e.pay(i.payinfo, function (t) {
                  "requestPayment:ok" == t.errMsg && s.complete(a);
                }) : "credit" == a ? e.confirm("确认要支付吗?", function () {
                  s.complete(a);
                }, function () { }) : "cash" == a ? e.confirm("确认要使用货到付款吗?", function () {
                  s.complete(a);
                }, function () { }) : s.complete(a) : o.toast(s, t.message);
              }, !0, !0);
            
            } else {
              
              e.post("order/pay/checkstock", {
                id: s.data.options.id
              }, function (t) {
                0 == t.error ? "wechat" == a ? e.pay(i.payinfo, function (t) {
                  "requestPayment:ok" == t.errMsg && s.complete(a);
                }) : "credit" == a ? e.confirm("确认要支付吗?", function () {
                  s.complete(a);
                }, function () { }) : "cash" == a ? e.confirm("确认要使用货到付款吗?", function () {
                  s.complete(a);
                }, function () { }) : s.complete(a) : o.toast(s, t.message);
              }, !0, !0);
            }
          }
        }, fail(res) {
          console.log(res, 88888)
        }
      });


      
    },

    complete: function(t) {
        var a = this;
   
        e.post("order/pay/complete", {
            id: a.data.options.id,
            type: t
        }, function(t) {
          console.log(a.data.options.id,888)
            if(t.error=="0")
            {
              a.sendmessage(a.data.options.id)
            }
            if (0 != t.error) o.toast(a, t.message); else {
          
                wx.setNavigationBarTitle({
                    title: "支付成功"
                });

             




                var e = Array.isArray(t.ordervirtual);
                a.setData({
                    success: !0,
                    successData: t,
                    order: t.order,
                    ordervirtual: t.ordervirtual,
                    ordervirtualtype: e
                });
            }
        }, !0, !0);
    },
    sendmessage:function(id)
    {
      var a = this;
      var id=id;
      console.log(id,99999);
      e.post("order/index/sendmessage", {
       id:id
      }, function (res) {
      
      })
    },
    shop: function(t) {
        0 == e.pdata(t).id ? this.setData({
            shop: 1
        }) : this.setData({
            shop: 0
        });
    },
    phone: function(t) {
        e.phone(t);
    },
    closecoupon: function() {
        this.setData({
            coupon: !1
        });
    }
});